package generator.website;

import java.util.Iterator;
import org.eclipse.emf.common.util.EList;
import CarProject.*;
import CarProject.impl.*;;

public class ModifyAction
{
  protected static String nl;
  public static synchronized ModifyAction create(String lineSeparator)
  {
    nl = lineSeparator;
    ModifyAction result = new ModifyAction();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = " " + NL + "" + NL + "" + NL + "import javax.servlet.http.HttpServletRequest;" + NL + "import javax.servlet.http.HttpServletResponse;" + NL + "import javax.swing.JOptionPane;" + NL + "" + NL + "import org.apache.struts.action.Action;" + NL + "import org.apache.struts.action.ActionForm;" + NL + "import org.apache.struts.action.ActionForward;" + NL + "import org.apache.struts.action.ActionMapping;" + NL + "" + NL + "" + NL + "public class Modify";
  protected final String TEXT_2 = "Action extends Action {" + NL + "\tpublic ActionForward execute(ActionMapping mapping, ActionForm form," + NL + "\t\t\tHttpServletRequest request, HttpServletResponse response) {" + NL + "\t\t";
  protected final String TEXT_3 = NL + "\t\tfinal Modify";
  protected final String TEXT_4 = "ValidationForm lForm = (Modify";
  protected final String TEXT_5 = "ValidationForm) form;" + NL + "" + NL + "" + NL + "\t\t\t";
  protected final String TEXT_6 = NL + "\t\tfinal ";
  protected final String TEXT_7 = " ";
  protected final String TEXT_8 = " = lForm.get";
  protected final String TEXT_9 = "();" + NL + "\t\t";
  protected final String TEXT_10 = NL + "\t\t" + NL + "" + NL + "\t\t// pour cr?er  un nouveau  DAO " + NL + "\t\tDAO";
  protected final String TEXT_11 = " bd = new DAO";
  protected final String TEXT_12 = "();" + NL + "\t\tString lError = bd.modify";
  protected final String TEXT_13 = "(";
  protected final String TEXT_14 = ",";
  protected final String TEXT_15 = ");" + NL + "" + NL + "" + NL + "\t\t" + NL + "\t\tif (lError == null) {" + NL + "\t\t\treturn mapping.findForward(\"success\");" + NL + "\t\t} else {" + NL + "\t\t\tJOptionPane.showMessageDialog(null,\"on a une erreur du dao pour l'ajout en base de donn?es\");;" + NL + "\t\t\treturn mapping.findForward(\"error\");" + NL + "\t\t}" + NL + "\t}" + NL + "}";

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
     Table laTable = (Table) argument;
    String type=null;
    EList<Champs> listChamps = laTable.getPossede();
    stringBuffer.append(TEXT_1);
    stringBuffer.append(laTable.getNom());
    stringBuffer.append(TEXT_2);
    int i;
    stringBuffer.append(TEXT_3);
    stringBuffer.append(laTable.getNom());
    stringBuffer.append(TEXT_4);
    stringBuffer.append(laTable.getNom());
    stringBuffer.append(TEXT_5);
    for (i = 0;i<listChamps.size();i++){
			ChampsImpl cp = (ChampsImpl) laTable.getPossede().get(i);
	if(((ChampsImpl)laTable.getPossede().get(i)).getType().getValue()== EnumType.VARCHAR){ type = "String"; } else { type = "long"; }
	
    stringBuffer.append(TEXT_6);
    stringBuffer.append(type);
    stringBuffer.append(TEXT_7);
    stringBuffer.append(((ChampsImpl)laTable.getPossede().get(i)).getNom());
    stringBuffer.append(TEXT_8);
    stringBuffer.append(Character.toUpperCase(cp.getNom().charAt(0)));
    stringBuffer.append(((ChampsImpl)laTable.getPossede().get(i)).getNom().substring(1));
    stringBuffer.append(TEXT_9);
    
		}
		
    stringBuffer.append(TEXT_10);
    stringBuffer.append(laTable.getNom());
    stringBuffer.append(TEXT_11);
    stringBuffer.append(laTable.getNom());
    stringBuffer.append(TEXT_12);
    stringBuffer.append(laTable.getNom());
    stringBuffer.append(TEXT_13);
    
					for (i = 0;i<listChamps.size()-1;i++){
	if(((ChampsImpl)laTable.getPossede().get(i)).getType().getValue()== EnumType.VARCHAR){ type = "String"; } else { type = "long"; }
    stringBuffer.append(
	((ChampsImpl)laTable.getPossede().get(i)).getNom());
    stringBuffer.append(TEXT_14);
    
	}
    stringBuffer.append(((ChampsImpl)laTable.getPossede().get(i)).getNom());
    stringBuffer.append(TEXT_15);
    return stringBuffer.toString();
  }
}
